package com.tarena.lbs.user.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.enums.PassportResultEnum;
import com.tarena.lbs.common.enums.Roles;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.common.utils.AuthenticationContextUtils;
import com.tarena.lbs.user.dao.repository.AdminRepository;
import com.tarena.lbs.user.dao.repository.UserGroupRepository;
import com.tarena.lbs.user.pojo.param.UserGroupParam;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.po.UserGroupPO;
import com.tarena.lbs.user.pojo.query.UserGroupQuery;
import com.tarena.lbs.user.pojo.vo.UserGroupVO;
import com.tarena.lbs.user.service.UserGroupService;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import com.tarena.lbs.base.common.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.nio.file.attribute.UserPrincipal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class UserGroupServiceImpl implements UserGroupService {
    @Autowired
    private UserGroupRepository userGroupRepository;
    @Autowired
    private AdminRepository adminRepository;
    @Override
    public void save(UserGroupParam param) throws BusinessException{
        log.info("保存人群数据:{}",param);
        UserPrinciple currentUser = AuthenticationContextUtils.get();
        log.info("当前用户认证信息:{}",currentUser);
        Asserts.isTrue(currentUser==null, new BusinessException("-2","用户认证解析失败"));
        AdminPO adminPO = adminRepository.getAdminById(currentUser.getId());
        if (adminPO!=null){
            log.info("当前用户信息:Id:{},状态:{}",adminPO.getId(),adminPO.getAccountStatus());
            Asserts.isTrue(adminPO.getAccountStatus()==1, new BusinessException("-1","用户被禁用"));
            UserGroupPO po = new UserGroupPO();
            BeanUtils.copyProperties(param,po);
            po.setBusinessId(adminPO.getBusinessId());
            po.setCreateAt(new Date());
            log.info("保存人群数据:{}",po);
            userGroupRepository.save(po);
        }else {
            throw new BusinessException("-1","商家用户不存在");
        }
    }


    @Override
    public PageResult<UserGroupVO> pageList(UserGroupQuery  query) throws BusinessException {
        //1.封装一个分页假数据 pageNo pageSize total 装配一下所有list
        PageResult<UserGroupVO> voPage=new PageResult<>();
        voPage.setPageNo(1);
        voPage.setPageSize(10);
        voPage.setTotal(100L);
        //2.调用仓储层查询所有人群数据 关注一下 人群所属的商家
        //解析认证对象 拿到id 查询账号详情 拿到商家id
        UserPrinciple userPrinciple = AuthenticationContextUtils.get();
        Asserts.isTrue(userPrinciple==null,new BusinessException("-2","用户认证解析失败"));
        AdminPO adminPO = adminRepository.getAdminById(userPrinciple.getId());
        Asserts.isTrue(adminPO==null,new BusinessException("-2","商家账号信息不存在"));
        Integer businessId=adminPO.getBusinessId();
        query.setBusinessId(businessId);
        List<UserGroupPO> pos= userGroupRepository.getUserGroups(query);
        //3 封装返回值
        List<UserGroupVO> vos=null;
        if (CollectionUtils.isNotEmpty(pos)){
            log.info("查询的商家Id:{}",businessId);
            vos=pos.stream().map(po->{
                UserGroupVO vo=new UserGroupVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        voPage.setObjects(vos);
        return voPage;
    }


    @Override
    public void delete(Integer id) throws BusinessException{
        UserPrinciple userPrinciple = AuthenticationContextUtils.get();
        Asserts.isTrue(userPrinciple==null,new BusinessException("-2","用户认证解析失败"));
        if (userPrinciple.getRole()!= Roles.SHOP){
            throw new BusinessException("-1","用户权限不足");
        }
        userGroupRepository.delete(id);
    }
}
